﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace Snowdreamist.Web.Rss.V2
{
    public class Parser
    {
        public Feed Parse(XmlReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");

            XDocument xDoc = XDocument.Load(reader);
            // feed
            Feed feed = new Feed();
            // verify rss root
            var rssElm = xDoc.Element("rss");
            if (rssElm == null)
                throw new InvalidOperationException("Cannot find rss root element");
            var versionAttr = rssElm.Attribute("version");
            if (versionAttr == null)
                throw new InvalidOperationException("Cannot verify rss feed version");
            if (versionAttr.Value != "2.0")
                throw new InvalidOperationException("Not a v2.0 rss feed");
            // fetch channel
            var channelElm = rssElm.Element("channel");
            if (channelElm == null)
                throw new InvalidOperationException("Cannot find channel element");
            // title
            var channelTitleElm = channelElm.Element("title");
            if (channelTitleElm != null)
                feed.Channel.Title = channelTitleElm.Value;
            // description
            var channelDspElm = channelElm.Element("description");
            if (channelDspElm != null)
                feed.Channel.Description = channelDspElm.Value;
            // link
            var channelLinkElm = channelElm.Element("link");
            if (channelLinkElm != null)
                feed.Channel.Link = channelLinkElm.Value;
            // fetch all items
            var items = channelElm.Elements("item");
            if (items != null)
            {
                foreach (var itemElm in items)
                {
                    Item item = new Item();
                    // title
                    var itemTitleElm = itemElm.Element("title");
                    if (itemTitleElm != null)
                        item.Title = itemTitleElm.Value;
                    // link
                    var itemLinkElm = itemElm.Element("link");
                    if (itemLinkElm != null)
                        item.Link = itemLinkElm.Value;
                    // category
                    var itemCateElm = itemElm.Element("category");
                    if (itemCateElm != null)
                        item.Category = itemCateElm.Value;
                    // description
                    var itemDspElm = itemElm.Element("description");
                    if (itemDspElm != null)
                        item.Description = itemDspElm.Value;
                    // pubDate
                    var itemPubdataElm = itemElm.Element("pubDate");
                    if(itemPubdataElm != null && !string.IsNullOrWhiteSpace(itemPubdataElm.Value))
                    {
                        DateTime time;
                        if (DateTime.TryParse(itemPubdataElm.Value, out time))
                            item.PublishDate = time;
                    }
                    // guid
                    var itemGuidElm = itemElm.Element("guid");
                    if (itemGuidElm != null)
                        item.Guid = itemGuidElm.Value;
                    // append
                    feed.Channel.Items.Add(item);
                }
            }
            return feed;
        }
    }
}
